<script lang="ts">
  import { createEventDispatcher } from 'svelte';

  const dispatch = createEventDispatcher();

  export let checked: boolean = false;
  export let disabled: boolean = false;
  export let size: 'small' | 'medium' | 'large' = 'medium';

  function toggle(): void {
    if (!disabled) {
      dispatch('click', { checked });
    }
  }

  function handleKeyPress(event: KeyboardEvent): void {
    if (event.key === 'Enter' || event.key === ' ') {
      event.preventDefault();
      toggle();
    }
  }
</script>

<button 
  class="toggle-button" 
  class:checked={checked}
  class:disabled={disabled}
  class:size-small={size === 'small'}
  class:size-medium={size === 'medium'}
  class:size-large={size === 'large'}
  on:click={toggle}
  on:keypress={handleKeyPress}
  role="switch"
  aria-checked={checked}
  aria-disabled={disabled}
  tabindex={disabled ? -1 : 0}
>
  <div class="toggle-track">
    <div class="toggle-thumb"></div>
  </div>
  <slot />
</button>

<style>
  .toggle-button {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(52, 152, 219, 0.2);
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 8px;
    transition: all 0.3s ease;
    color: #2c3e50;
    font-size: 1rem;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  .toggle-button:not(.disabled):hover {
    background: rgba(255, 255, 255, 1);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }

  .toggle-button:not(.disabled):focus {
    outline: 2px solid rgba(52, 152, 219, 0.5);
    outline-offset: 2px;
  }

  .toggle-button.disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .toggle-track {
    position: relative;
    width: 50px;
    height: 24px;
    background: #ecf0f1;
    border-radius: 12px;
    transition: all 0.3s ease;
    border: 2px solid #bdc3c7;
  }

  .toggle-button.checked .toggle-track {
    background: linear-gradient(45deg, #2ecc71, #27ae60);
    border-color: #2ecc71;
  }

  .toggle-button.disabled .toggle-track {
    background: #bdc3c7;
    border-color: #95a5a6;
  }

  .toggle-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    background: #fff;
    border-radius: 50%;
    transition: all 0.3s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }

  .toggle-button.checked .toggle-thumb {
    transform: translateX(26px);
  }

  .toggle-button.disabled .toggle-thumb {
    background: #ecf0f1;
  }

  /* 尺寸变体 */
  .toggle-button.size-small .toggle-track {
    width: 40px;
    height: 20px;
  }

  .toggle-button.size-small .toggle-thumb {
    width: 12px;
    height: 12px;
  }

  .toggle-button.size-small.checked .toggle-thumb {
    transform: translateX(20px);
  }

  .toggle-button.size-large .toggle-track {
    width: 60px;
    height: 28px;
  }

  .toggle-button.size-large .toggle-thumb {
    width: 20px;
    height: 20px;
  }

  .toggle-button.size-large.checked .toggle-thumb {
    transform: translateX(32px);
  }

  /* 动画效果 */
  .toggle-button:not(.disabled) .toggle-track {
    animation: pulse 2s infinite;
  }

  @keyframes pulse {
    0% {
      box-shadow: 0 0 0 0 rgba(46, 204, 113, 0.4);
    }
    70% {
      box-shadow: 0 0 0 10px rgba(46, 204, 113, 0);
    }
    100% {
      box-shadow: 0 0 0 0 rgba(46, 204, 113, 0);
    }
  }

  .toggle-button.checked:not(.disabled) .toggle-track {
    animation: none;
  }

  /* 暗色主题 */
  :global(body.dark-mode) .toggle-button {
    background: rgba(52, 73, 94, 0.9);
    border-color: rgba(52, 152, 219, 0.3);
    color: #ecf0f1;
  }

  :global(body.dark-mode) .toggle-button:not(.disabled):hover {
    background: rgba(52, 73, 94, 1);
  }

  :global(body.dark-mode) .toggle-track {
    background: #34495e;
    border-color: #2c3e50;
  }

  :global(body.dark-mode) .toggle-button.checked .toggle-track {
    background: linear-gradient(45deg, #2ecc71, #27ae60);
    border-color: #2ecc71;
  }

  :global(body.dark-mode) .toggle-button.disabled .toggle-track {
    background: #2c3e50;
    border-color: #34495e;
  }

  :global(body.dark-mode) .toggle-thumb {
    background: #ecf0f1;
  }

  :global(body.dark-mode) .toggle-button.disabled .toggle-thumb {
    background: #95a5a6;
  }
</style> 